home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 23 code / Internet Config / IC 1.1 / ICProgKit1.1 / Source / ICComponent / ICComponent.p next >
Encoding:
Text File  |  1995-04-23  |  15.9 KB  |  524 lines  |  [TEXT/PJMM]

  1. unit ICComponent;
  2.  
  3. interface
  4.  
  5.     uses
  6.         Components;
  7.  
  8.     function Main (var params: ComponentParameters; storage: Handle): ComponentResult;
  9.  
  10. implementation
  11.  
  12.     uses
  13. {$ifc undefined THINK_Pascal}
  14.         Types, Files, Memory, Errors, ToolUtils, AppleTalk, Aliases, 
  15.  
  16.         ICTypes, ICKeys, 
  17. {$endc}
  18.         ICRSubs, ICRAPI, ICCAPI, ICComponentSelectors;
  19.  
  20.     type
  21.         globalsRecord = record
  22.                 self: ComponentInstance;
  23.                 current_target: ComponentInstance;
  24.                 inst: ICRRecord;
  25.             end;
  26.         globalsPtr = ^globalsRecord;
  27.         globalsHandle = ^globalsPtr;
  28.  
  29. (* Component Manager routines *)
  30.  
  31.     function ICCICanDo (globals: globalsHandle; selector: integer): ComponentResult;
  32.     (* Handle the Component Manager CanDo request.*)
  33.     begin
  34.         case selector of
  35.             kComponentVersionSelect..kComponentOpenSelect, kComponentTargetSelect, {}
  36.             kICC_first_select..kICC_last_select: 
  37.                 ICCICanDo := 1;
  38.             otherwise
  39.                 ICCICanDo := 0;
  40.         end; (* case *)
  41.     end; (* ICCICanDo *)
  42.  
  43.     function ICCIOpen (globals: globalsHandle; self: ComponentInstance): ComponentResult;
  44.     (* Handle the Component Manager Open request, mostly delayed until ICCStart. *)
  45.         var
  46.             err: ComponentResult;
  47.     begin
  48.         (* create our globals *)
  49.         globals := globalsHandle(NewHandle(sizeof(globalsRecord)));
  50.         err := MemError;
  51.  
  52.         if err = noErr then begin
  53.             globals^^.self := self;
  54.             globals^^.current_target := self;
  55.             (* tell the Component Manager about them *)
  56.             SetComponentInstanceStorage(self, Handle(globals));
  57.         end; (* if *)
  58.  
  59.         ICCIOpen := noErr;
  60.     end; (* ICCIOpen *)
  61.  
  62.     function ICCIClose (globals: globalsHandle; self: ComponentInstance): ComponentResult;
  63.     (* Handle the Component Manager Close request. *)
  64.         var
  65.             err: ComponentResult;
  66.     begin
  67.         err := noErr;
  68.         if globals <> nil then begin
  69. (* err := ICRStop(globals^^.inst); *)
  70.             DisposeHandle(Handle(globals));
  71.         end; (* if *)
  72.         ICCIClose := err;
  73.     end; (* ICCIClose *)
  74.  
  75.     function ICCITarget (globals: globalsHandle; new_target: ComponentInstance): ComponentResult;
  76.     (* Handle the Component Manager Target. *)
  77.         var
  78.             err: ComponentResult;
  79.     begin
  80.         globals^^.current_target := new_target;
  81.         ICCITarget := noErr;
  82.     end; (* ICCITarget *)
  83.  
  84. (* Internet Configuration specific routines *)
  85.  
  86.     function ICCIStart (globals: globalsHandle; creator: OSType): ICError;
  87.     (* Handle the start request, basically a replacement for the open because we need bonus data (creator). *)
  88.         var
  89.             err: OSErr;
  90.     begin
  91.         err := ICRStart(globals^^.inst, creator);
  92.         if err = noErr then begin
  93.             err := ICCDefaultFileName(globals^^.current_target, globals^^.inst.default_filename);
  94.         end; (* if *)
  95.         ICCIStart := err;
  96.     end; (* ICCIStart *)
  97.  
  98.     function ICCIStop (globals: globalsHandle): ICError;
  99.     begin
  100.         ICCIStop := ICRStop(globals^^.inst);
  101.     end; (* ICCIStop *)
  102.  
  103.     function ICCIFindConfigFile (globals: globalsHandle; count: integer; folders: ICDirSpecArrayPtr): ICError;
  104.     begin
  105.         ICCIFindConfigFile := ICRFindConfigFile(globals^^.inst, count, folders);
  106.     end; (* ICCIFindConfigFile *)
  107.  
  108.     function ICCIFindUserConfigFile (globals: globalsHandle; where: ICDirSpec): ICError;
  109.     begin
  110.         ICCIFindUserConfigFile := ICRFindUserConfigFile(globals^^.inst, where);
  111.     end; (* ICCIFindUserConfigFile *)
  112.  
  113.     function ICCISpecifyConfigFile (globals: globalsHandle; config: FSSpec): ICError;
  114.     begin
  115.         ICCISpecifyConfigFile := ICRSpecifyConfigFile(globals^^.inst, config);
  116.     end; (* ICCISpecifyConfigFile *)
  117.  
  118.     function ICCIGetSeed (globals: globalsHandle; var seed: longint): ICError;
  119.     begin
  120.         ICCIGetSeed := ICRGetSeed(globals^^.inst, seed);
  121.     end; (* ICCIGetSeed *)
  122.  
  123.     function ICCIGetPerm (globals: globalsHandle; var perm: icPerm): ICError;
  124.     begin
  125.         ICCIGetPerm := ICRGetPerm(globals^^.inst, perm);
  126.     end; (* ICCIGetPerm *)
  127.  
  128.     function ICCIBegin (globals: globalsHandle; perm: ICPerm): ICError;
  129.     begin
  130.         ICCIBegin := ICRBegin(globals^^.inst, perm);
  131.     end; (* ICCIBegin *)
  132.  
  133.     function ICCIForceInside (globals: globalsHandle; perm: ICPerm; var force_info: boolean): ICError;
  134.         var
  135.             err: ICError;
  136.             current_perm: icPerm;
  137.     begin
  138.         force_info := false;
  139.         current_perm := globals^^.inst.perm;
  140.         if (current_perm = perm) or ((current_perm = icReadWritePerm) and (perm = icReadOnlyPerm)) then begin
  141.             err := noErr;
  142.         end
  143.         else if current_perm = icNoPerm then begin
  144.             err := ICCBegin(globals^^.current_target, perm);
  145.             force_info := (err = noErr);
  146.         end
  147.         else begin
  148.             err := icPermErr;
  149.         end; (* if *)
  150.         ICCIForceInside := err;
  151.     end; (* ICCIForceInside *)
  152.  
  153.     function ICCIReleaseInside (globals: globalsHandle; force_info: boolean): ICError;
  154.     begin
  155.         if force_info then begin
  156.             ICCIReleaseInside := ICCEnd(globals^^.current_target);
  157.         end
  158.         else begin
  159.             ICCIReleaseInside := noErr;
  160.         end; (* if *)
  161.     end; (* ICCIReleaseInside *)
  162.  
  163.     (* The set and get routines are significantly complicated by the fact that the ICR versions can call *)
  164.     (* ICBegin if the calling program hasn't done so already. The problem with this is that override *)
  165.     (* component would not see these calls because the ICR code calls the code directly. The solution *)
  166.     (* is for the component to calls these routines itself (which sends them through the override *)
  167.     (* components. Oh god, this is confusing and it's getting worse as I try to maintain compatibility*)
  168.     (* and putting the cache in is going to be even worse. *)
  169.  
  170.     function ICCIGetPref (globals: globalsHandle; key: Str255; var attr: ICAttr; buf: Ptr; var size: longint): ICError;
  171.         var
  172.             err: ICError;
  173.             err2: ICError;
  174.             force_info: boolean;
  175.     begin
  176.         err := ICCIForceInside(globals, icReadOnlyPerm, force_info);
  177.         if err = noErr then begin
  178.             err := ICRGetPref(globals^^.inst, key, attr, buf, size);
  179.         end; (* if *)
  180.         err2 := ICCIReleaseInside(globals, force_info);
  181.         if err = noErr then begin
  182.             err := err2;
  183.         end; (* if *)
  184.         ICCIGetPref := err;
  185.     end; (* ICCIGetPref *)
  186.  
  187.     function ICCISetPref (globals: globalsHandle; key: Str255; attr: ICAttr; buf: Ptr; size: longint): ICError;
  188.         var
  189.             err: ICError;
  190.             err2: ICError;
  191.             force_info: boolean;
  192.     begin
  193.         err := ICCIForceInside(globals, icReadWritePerm, force_info);
  194.         if err = noErr then begin
  195.             err := ICRSetPref(globals^^.inst, key, attr, buf, size);
  196.         end; (* if *)
  197.         err2 := ICCIReleaseInside(globals, force_info);
  198.         if err = noErr then begin
  199.             err := err2;
  200.         end; (* if *)
  201.         ICCISetPref := err;
  202.     end; (* ICCISetPref *)
  203.  
  204.     function ICCIGetPrefHandle (globals: globalsHandle; key: Str255; var attr: ICAttr; var prefh: Handle): ICError;
  205.         var
  206.             err: ICError;
  207.             prefsize: longint;
  208.             force_info: boolean;
  209.             err2: ICError;
  210.     begin
  211.         prefh := nil;
  212.         prefsize := 0;
  213.         err := ICCIForceInside(globals, icReadOnlyPerm, force_info);
  214.         if err = noErr then begin
  215.             err := ICCGetPref(globals^^.current_target, key, attr, nil, prefsize);
  216.         end; (* if *)
  217.         if err <> noErr then begin
  218.             prefsize := 0;
  219.         end; (* if *)
  220.         prefh := NewHandle(prefsize);
  221.         err := MemError;
  222.         if err = noErr then begin
  223.             HLock(prefh);
  224.             err := ICCGetPref(globals^^.current_target, key, attr, prefh^, prefsize);
  225.             if err = icPrefNotFoundErr then begin
  226.                 attr := 0;
  227.                 err := noErr;
  228.             end; (* if *)
  229.             HUnlock(prefh);
  230.         end; (* if *)
  231.         if err <> noErr then begin
  232.             if prefh <> nil then begin
  233.                 DisposeHandle(prefh);
  234.             end; (* if *)
  235.             prefh := nil;
  236.         end; (* if *)
  237.         err2 := ICCIReleaseInside(globals, force_info);
  238.         if err = noErr then begin
  239.             err := err2;
  240.         end; (* if *)
  241.         ICCIGetPrefHandle := err;
  242.     end; (* ICCIGetPrefHandle *)
  243.  
  244.     function ICCISetPrefHandle (globals: globalsHandle; key: Str255; attr: ICAttr; prefh: Handle): ICError;
  245.         var
  246.             s: SignedByte;
  247.             err: ICError;
  248.     begin
  249.         err := noErr;
  250.         if prefh <> nil then begin
  251.             if prefh^ = nil then begin
  252.                 err := paramErr;
  253.             end; (* if *)
  254.             if err = noErr then begin
  255.                 s := HGetState(prefh);
  256.                 HLock(prefh);
  257.                 err := ICCSetPref(globals^^.current_target, key, attr, prefh^, GetHandleSize(prefh));
  258.                 HSetState(prefh, s);
  259.             end; (* if *)
  260.         end else begin
  261.             err := ICCSetPref(globals^^.current_target, key, attr, nil, 0);
  262.         end; (* if *)
  263.         ICCISetPrefHandle := err;
  264.     end; (* ICCISetPrefHandle *)
  265.  
  266.     function ICCICountPref (globals: globalsHandle; var count: longint): ICError;
  267.     begin
  268.         ICCICountPref := ICRCountPref(globals^^.inst, count);
  269.     end; (* ICCICountPref*)
  270.  
  271.     function ICCIGetIndPref (globals: globalsHandle; n: longint; var key: Str255): ICError;
  272.     begin
  273.         ICCIGetIndPref := ICRGetIndPref(globals^^.inst, n, key);
  274.     end; (* ICCIGetIndPref *)
  275.  
  276.     function ICCIDeletePref (globals: globalsHandle; key: Str255): ICError;
  277.     begin
  278.         ICCIDeletePref := ICRDeletePref(globals^^.inst, key);
  279.     end; (* ICCIDeletePref *)
  280.  
  281.     function ICCIEnd (globals: globalsHandle): ICError;
  282.     begin
  283.         ICCIEnd := ICREnd(globals^^.inst);
  284.     end; (* ICCIEnd *)
  285.  
  286.     function ICCIDefaultFile (globals: globalsHandle; var name: Str63): ICError;
  287.         var
  288.             err: ICError;
  289.             junk: ICError;
  290.             refnum: integer;
  291.             strh: StringHandle;
  292.     begin
  293.         err := noErr;
  294.         name := ICdefault_file_name;
  295.         refnum := OpenComponentResFile(Component(globals^^.self));
  296.         if refnum <= 0 then begin
  297.             err := resNotFound;
  298.         end; (* if *)
  299.         if err = noErr then begin
  300.             strh := GetString(ICdefault_file_name_ID);
  301.             if strh = nil then begin
  302.                 err := resNotFound;
  303.             end
  304.             else begin
  305.                 name := strh^^;
  306.             end; (* if *)
  307.             junk := CloseComponentResFile(refnum);
  308.         end; (* if *)
  309.         ICCIDefaultFile := err;
  310.     end; (* ICCIDefaultFile *)
  311.  
  312.     function ICCIEditPreferences (globals: globalsHandle; key: Str255): ICError;
  313.     begin
  314.         ICCIEditPreferences := ICREditPreferences(globals^^.inst, key);
  315.     end; (* ICCIEditPreferences *)
  316.  
  317.     function ICCIParseURL (globals: globalsHandle; hint: Str255; data: Ptr; len: longint; var selStart, selEnd: longint; url: Handle): ICError;
  318.     begin
  319.         ICCIParseURL := ICRParseURL(globals^^.inst, hint, data, len, selStart, selEnd, url);
  320.     end; (* ICCIParseURL *)
  321.  
  322.     function ICCILaunchURL (globals: globalsHandle; hint: Str255; data: Ptr; len: longint; var selStart, selEnd: longint): ICError;
  323.         var
  324.             err: ICError;
  325.             urlh: Handle;
  326.             helper: ICAppSpec;
  327.             scheme: Str255;
  328.             junk_attr: longint;
  329.             size: longint;
  330.     begin
  331.         urlh := NewHandle(0);
  332.         err := MemError;
  333.         if err = noErr then begin
  334.             err := ICCParseURL(globals^^.current_target, hint, data, len, selStart, selEnd, urlh);
  335.         end; (* if *)
  336.         if err = noErr then begin
  337.             err := FindScheme(urlh, scheme);
  338.         end; (* if *)
  339.         if err = noErr then begin
  340.             size := sizeof(helper);
  341.             err := ICCGetPref(globals^^.current_target, concat(kICHelper, scheme), junk_attr, @helper, size);
  342.         end; (* if *)
  343.         if err = noErr then begin
  344.             err := LaunchURL(helper.fCreator, urlh);
  345.         end; (* if *)
  346.         if urlh <> nil then begin
  347.             DisposeHandle(urlh);
  348.         end; (* if *)
  349.         ICCILaunchURL := err;
  350.     end; (* ICCILaunchURL *)
  351.  
  352.     function ICCIMapFilename (globals: globalsHandle; filename: Str255; var entry: ICMapEntry): ICError;
  353.         var
  354.             err: ICError;
  355.             entries: Handle;
  356.             junk_attr: ICAttr;
  357.     begin
  358.         err := noErr;
  359.         if filename = '' then begin
  360.             err := paramErr;
  361.         end; (* if *)
  362.         if err = noErr then begin
  363.             err := ICCGetPrefHandle(globals^^.current_target, kICMapping, junk_attr, entries);
  364.         end; (* if *)
  365.         if err = noErr then begin
  366.             err := ICCMapEntriesFilename(globals^^.current_target, entries, filename, entry);
  367.             DisposeHandle(entries);
  368.         end; (* if *)
  369.         ICCIMapFilename := err;
  370.     end; (* ICCIMapFilename *)
  371.  
  372.     function ICCIMapTypeCreator (globals: globalsHandle; fType, fCreator: OSType; filename: Str255; var entry: ICMapEntry): ICError;
  373.         var
  374.             err: ICError;
  375.             entries: Handle;
  376.             junk_attr: ICAttr;
  377.     begin
  378.         err := ICCGetPrefHandle(globals^^.current_target, kICMapping, junk_attr, entries);
  379.         if err = noErr then begin
  380.             err := ICCMapEntriesTypeCreator(globals^^.current_target, entries, fType, fCreator, filename, entry);
  381.             DisposeHandle(entries);
  382.         end; (* if *)
  383.         ICCIMapTypeCreator := err;
  384.     end; (* ICCIMapTypeCreator *)
  385.  
  386.     function ICCIMapEntriesFilename (globals: globalsHandle; entries: Handle; filename: Str255; var entry: ICMapEntry): ICError;
  387.     begin
  388.         ICCIMapEntriesFilename := ICRMapEntriesFilename(globals^^.inst, entries, filename, entry);
  389.     end; (* ICCIMapEntriesFilename *)
  390.  
  391.     function ICCIMapEntriesTypeCreator (globals: globalsHandle; entries: Handle; fType, fCreator: OSType; filename: Str255; var entry: ICMapEntry): ICError;
  392.     begin
  393.         ICCIMapEntriesTypeCreator := ICRMapEntriesTypeCreator(globals^^.inst, entries, fType, fCreator, filename, entry);
  394.     end; (* ICCIMapEntriesTypeCreator *)
  395.  
  396.     function ICCICountMapEntries (globals: globalsHandle; entries: Handle; var count: longint): ICError;
  397.     begin
  398.         ICCICountMapEntries := ICRCountMapEntries(globals^^.inst, entries, count);
  399.     end; (* ICCICountMapEntries *)
  400.  
  401.     function ICCIGetIndMapEntry (globals: globalsHandle; entries: Handle; ndx: longint; var pos: longint; var entry: ICMapEntry): ICError;
  402.     begin
  403.         ICCIGetIndMapEntry := ICRGetIndMapEntry(globals^^.inst, entries, ndx, pos, entry);
  404.     end; (* ICCIGetIndMapEntry *)
  405.  
  406.     function ICCIGetMapEntry (globals: globalsHandle; entries: Handle; pos: longInt; var entry: ICMapEntry): ICError;
  407.     begin
  408.         ICCIGetMapEntry := ICRGetMapEntry(globals^^.inst, entries, pos, entry);
  409.     end; (* ICCIGetMapEntry *)
  410.  
  411.     function ICCISetMapEntry (globals: globalsHandle; entries: Handle; pos: longInt; var entry: ICMapEntry): ICError;
  412.     begin
  413.         ICCISetMapEntry := ICRSetMapEntry(globals^^.inst, entries, pos, entry);
  414.     end; (* ICCISetMapEntry *)
  415.  
  416.     function ICCIDeleteMapEntry (globals: globalsHandle; entries: Handle; pos: longint): ICError;
  417.     begin
  418.         ICCIDeleteMapEntry := ICRDeleteMapEntry(globals^^.inst, entries, pos);
  419.     end; (* ICCIDeleteMapEntry *)
  420.  
  421.     function ICCIAddMapEntry (globals: globalsHandle; entries: Handle; var entry: ICMapEntry): ICError;
  422.     begin
  423.         ICCIAddMapEntry := ICRAddMapEntry(globals^^.inst, entries, entry);
  424.     end; (* ICCIAddMapEntry *)
  425.  
  426. (* ----- Component Entry Point ----- *)
  427.  
  428.     function Main (var params: ComponentParameters; storage: Handle): ComponentResult;
  429.     (* Component entry point.  It's pretty neat IMHO. *)
  430.         var
  431.             proc: ProcPtr;
  432.             s: signedByte;
  433.     begin
  434.         proc := nil;
  435.         case params.what of
  436.             (* Component Manager stuff *)
  437.             kComponentVersionSelect: 
  438.                 Main := internetConfigurationComponentInterfaceVersion;
  439.             kComponentCanDoSelect: 
  440.                 proc := @ICCICanDo;
  441.             kComponentOpenSelect: 
  442.                 proc := @ICCIOpen;
  443.             (* Component Manager stuff *)
  444.             kComponentCloseSelect: 
  445.                 proc := @ICCIClose;
  446.             kComponentTargetSelect: 
  447.                 proc := @ICCITarget;
  448.             (* this component type stuff *)
  449.             kICCStart: 
  450.                 proc := @ICCIStart;
  451.             kICCStop: 
  452.                 proc := @ICCIStop;
  453.             kICCFindConfigFile: 
  454.                 proc := @ICCIFindConfigFile;
  455.             kICCSpecifyConfigFile: 
  456.                 proc := @ICCISpecifyConfigFile;
  457.             kICCGetSeed: 
  458.                 proc := @ICCIGetSeed;
  459.             kICCGetPerm: 
  460.                 proc := @ICCIGetPerm;
  461.             kICCBegin: 
  462.                 proc := @ICCIBegin;
  463.             kICCGetPref: 
  464.                 proc := @ICCIGetPref;
  465.             kICCSetPref: 
  466.                 proc := @ICCISetPref;
  467.             kICCCountPref: 
  468.                 proc := @ICCICountPref;
  469.             kICCGetIndPref: 
  470.                 proc := @ICCIGetIndPref;
  471.             kICCDeletePref: 
  472.                 proc := @ICCIDeletePref;
  473.             kICCEnd: 
  474.                 proc := @ICCIEnd;
  475.             kICCDefaultFile: 
  476.                 proc := @ICCIDefaultFile;
  477.             kICCFindUserConfigFile: 
  478.                 proc := @ICCIFindUserConfigFile;
  479.             kICCEditPreferences: 
  480.                 proc := @ICCIEditPreferences;
  481.             kICCParseURL: 
  482.                 proc := @ICCIParseURL;
  483.             kICCLaunchURL: 
  484.                 proc := @ICCILaunchURL;
  485.             kICCCountMapEntries: 
  486.                 proc := @ICCICountMapEntries;
  487.             kICCGetIndMapEntry: 
  488.                 proc := @ICCIGetIndMapEntry;
  489.             kICCGetMapEntry: 
  490.                 proc := @ICCIGetMapEntry;
  491.             kICCSetMapEntry: 
  492.                 proc := @ICCISetMapEntry;
  493.             kICCDeleteMapEntry: 
  494.                 proc := @ICCIDeleteMapEntry;
  495.             kICCAddMapEntry: 
  496.                 proc := @ICCIAddMapEntry;
  497.             kICCMapFilename:
  498.                 proc := @ICCIMapFilename;
  499.             kICCMapTypeCreator: 
  500.                 proc := @ICCIMapTypeCreator;
  501.             kICCGetPrefHandle: 
  502.                 proc := @ICCIGetPrefHandle;
  503.             kICCSetPrefHandle: 
  504.                 proc := @ICCISetPrefHandle;
  505.             kICCMapEntriesFilename: 
  506.                 proc := @ICCIMapEntriesFilename;
  507.             kICCMapEntriesTypeCreator: 
  508.                 proc := @ICCIMapEntriesTypeCreator;
  509.             otherwise
  510.                 Main := badComponentSelector;
  511.         end; (* case *)
  512.         if proc <> nil then begin
  513.             if storage <> nil then begin
  514.                 s := HGetState(storage);
  515.                 HLock(storage);
  516.             end; (* if *)
  517.             Main := CallComponentFunctionWithStorage(storage, params, proc);
  518.             if (storage <> nil) and (params.what <> kComponentCloseSelect) then begin
  519.                 HSetState(storage, s);
  520.             end; (* if *)
  521.         end; (* if *)
  522.     end; (* Main *)
  523.  
  524. end. (* ICComponent *)